package com.gm.wholesale.module.setting.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.commonlib.views.dialog.loadingDialog.LoadingDialog;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.bluetooth.DeviceManger;
import com.gm.wholesale.bluetooth.bean.DeviceBean;
import com.gm.wholesale.bluetooth.bean.WeightBean;
import com.gm.wholesale.bluetooth.interfaces.OnConnectionBluListener;
import com.gm.wholesale.bluetooth.printBlue.OrderPrintBluethManger;
import com.gm.wholesale.constant.Constant;
import com.gm.wholesale.module.setting.view.BluetoothListView;
import com.gm.wholesale.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

public class OrderPrintBlueListFragment extends BaseFragment {
    private BluetoothListView mPrintBlueDeviceView;
    private boolean mIsToConnection;

   private OrderPrintBluethManger mOrderPrintBluethManger;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mPrintBlueDeviceView=null;
        mPrintBlueDeviceView = new BluetoothListView(getActivity());
        showPrintBlueDevices();
        return mPrintBlueDeviceView;

    }
    private void showPrintBlueDevices() {
        mOrderPrintBluethManger=   OrderPrintBluethManger.newInstance(getContext());
        List<String> filter=new ArrayList<>();
        filter.add(Constant.ORDER_PRINT_BLU);
        mPrintBlueDeviceView.showDevice(filter,getPrintConDevice());
        mPrintBlueDeviceView.setOnItemClickListener(deviceBean -> {

            if (deviceBean.getState() == 1) {
                mIsToConnection=false;
                ToastUtil.showToast("已断开连接");

                mPrintBlueDeviceView.notifyDataSetChanged("");
                DeviceManger.getDeviceManger().cleanPrintDevice();
            } else {
                mIsToConnection=true;

                mOrderPrintBluethManger.setOnConnectionListener(new OnConnectionBluListener() {
                    @Override
                    public void connection(DeviceBean deviceBean) {
                        if (deviceBean!=null){
                            DeviceManger.getDeviceManger().savePrintDevice(deviceBean);
                        }
                        LoadingDialog.newLoadingDialog().hid();
                        ToastUtil.showToast("连接成功");
                        mPrintBlueDeviceView.notifyDataSetChanged(deviceBean.getAddress());
                    }

                    @Override
                    public void connectionFail() {
                        if (mIsToConnection){
                            ViewUtil.showPrintError();
                        }
                        LoadingDialog.newLoadingDialog().hid();
                    }

                    @Override
                    public void getNumber(WeightBean weightBean) {

                    }
                });
                mOrderPrintBluethManger.connectByDevice(getContext(),deviceBean.getAddress());
            }

        });



    }

    private String getPrintConDevice() {
        String connectionDevice = null;
        if (mOrderPrintBluethManger.getConnectionState() == 3) {
            connectionDevice = mOrderPrintBluethManger.getConnectionDevice();
        }
        return connectionDevice;
    }

    @Override
    public void onDetach() {
        if (mPrintBlueDeviceView!=null){
            mPrintBlueDeviceView.stop();
        }
        super.onDetach();
    }


}
